# -*- coding: utf-8 -*-
# author: sixmillions
# date: 2022/4/3

'''
数组创建
'''

import numpy as np

'''
1. 通过list创建numpy的array
'''
list_1 = [1, 2, 3, 4]

# 一维数组
array_1 = np.array(list_1)

print(1, array_1)  # 1 [1 2 3 4]
print(2, type(array_1))  # 2 <class 'numpy.ndarray'>

# 二维数组
list_2 = [5, 6, 7, 8]
array_2 = np.array([list_1, list_2])

print(3, array_2)  # 3 [[1 2 3 4] \n [5 6 7 8]]

# 维度
print(4, array_2.shape)  # 4 (2, 4)  表示两行四列

# 元素个数
print(5, array_2.size)  # 5 8

# 元素类型
print(6, array_2.dtype)  # 6 int32

'''
通过arrange创建
'''

array_3 = np.arange(1, 10)
print(7, array_3)  # 7 [1 2 3 4 5 6 7 8 9]

array_4 = np.arange(1, 10, 2)
print(8, array_4)  # 8 [1 3 5 7 9]

'''
特殊array
'''
# 全零
print(9, np.zeros(5))  # 9 [0. 0. 0. 0. 0.]
# 指定维度
print(10, np.zeros([2, 3]))

# 单位矩阵
print(11, np.eye(4))
print(12, np.eye(4).dtype)  # 12 float64

'''
array访问
'''

# 一维数组，和python的list切片一样
array_5 = np.arange(1, 10)
print(13, array_5[2])  # 13 3
print(14, array_5[2:])  # 14 [3 4 5 6 7 8 9]
print(15, array_5[3:6])  # 15 [4 5 6]

# 二维数组
array_6 = np.arange(1, 10).reshape(3, 3)
print(16, array_6)
print(17, array_6.shape)  # 17 (3, 3)
print(18, array_6[2][2])  # 18 9  第三行第三列
# 上面简写成
print(19, array_6[2, 2])  # 19 9
# 切片出一个数组
print(20, array_6[1:3, 1:2])  # 行是第二三行，列是第二列，索引从0开始
print(21, array_6[:3, 1:2])
print(22, array_6[:, 1:])
